﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class ApplyTypeController : Controller
    {
        private DbEntity db = new DbEntity();

        [IsAdmin]
        public ViewResult Index()
        {
            return View(db.ApplyTypes.ToList());
        }

        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Create(ApplyType applytype)
        {
            if (ModelState.IsValid)
            {
                //判断类型是否已经存在
                foreach (var at in db.ApplyTypes)
                {
                    if (at.TypeName == applytype.TypeName)
                    {
                        ViewData["error"] = "请类型名称已经存在";
                        return View(applytype);
                    }
                }
                db.ApplyTypes.Add(applytype);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(applytype);
        }

        //get,编辑
        [IsAdmin]
        public ActionResult Edit(int id)
        {
            ApplyType applytype = db.ApplyTypes.Find(id);
            return View(applytype);
        }

        //post,编辑
        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(ApplyType applytype)
        {
            if (ModelState.IsValid)
            {
                //判断类型是否已经存在
                foreach (var at in db.ApplyTypes)
                {
                    if (at.TypeName == applytype.TypeName && at.ApplyTypeID != applytype.ApplyTypeID)
                    {
                        ViewData["error"] = "请类型名称已经存在";
                        return View(applytype);
                    }
                }
                //执行编辑
                var sql = string.Format("update [applyTypes] set [typeName]='{0}' where [applyTypeID]='{1}'",
                    applytype.TypeName, applytype.ApplyTypeID);
                var n = db.Database.ExecuteSqlCommand(sql);
                if (n == 0)
                {
                    ViewData["error"] = "保存失败";
                    return View(applytype);
                }

                return RedirectToAction("Index");
            }
            return View(applytype);
        }

        //get,删除
        [IsAdmin]
        public ActionResult Delete(int id)
        {
            ApplyType applytype = db.ApplyTypes.Find(id);
            return View(applytype);
        }

        //post,删除
        [IsAdmin]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ApplyType applytype = db.ApplyTypes.Find(id);
            db.ApplyTypes.Remove(applytype);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}